﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VietKinsNutrition.Bussiness;
using VietKinsNutrition.Common;

namespace VietKinsNutrition.MoRong
{
    public partial class FrmChonthucdontuan : DevExpress.XtraEditors.XtraForm
    {
        ClsTblThamsodauvaoBLL ClsTblThamsodauvaobll = new ClsTblThamsodauvaoBLL();
        ClsThucdon_monanBLL ClsThucdon_monanbll = new ClsThucdon_monanBLL();
        ClsTblDm_BuaantrongngayBLL ClsTblDm_Buaantrongngaybll = new ClsTblDm_BuaantrongngayBLL();
        public FrmChonthucdontuan()
        {
            InitializeComponent();
        }

        private void FrmChonthucdontuan_Load(object sender, EventArgs e)
        {
            datTu.EditValue = DateTime.Now;
            datDen.EditValue = DateTime.Now;
            ClsThucdon_monanbll.Bind_Khoilop(cboKhoi);
            UserComboBoxItem.SelectItem(cboKhoi, "1");
        }

        private void cboKhoi_EditValueChanged(object sender, EventArgs e)
        {
            grdHienthi.Bands[1].Caption = "Khối " + cboKhoi.Text;
            if (grdDS.RowCount > 0)
            {
                try
                {
                    this.ClsThucdon_monanbll.BingSource_DataGridThucdontuan(grdHienthi, gridControl1, int.Parse(UserComboBoxItem.GetItemValue(cboKhoi)), DateTime.Parse(grdDS.GetFocusedRowCellValue("Tungay").ToString()));
                }
                catch { }
            }
        }

        List<DateTime> BgDate;
        List<DateTime> EndDate;

        private void LoadArrDatetime(DateTime BeginDatetime, DateTime EndDateTime)
        {
            BgDate = new List<DateTime>();
            EndDate = new List<DateTime>();
            int i = 0;
            DateTime TG = BeginDatetime;
            while (DateTime.Compare(TG, EndDateTime) <= 0)
            {
                BgDate.Add(GetDates(TG, "0"));
                EndDate.Add(BgDate[i].AddDays(5));
                TG = EndDate[i].AddDays(2);
                i = i + 1;
            }
        }

        private DateTime GetDates(DateTime dDateTime, string Var)
        {
            System.DateTime stDate; System.DateTime endDate;
            int trunggian = 0;
            double offset = 0;
            switch (dDateTime.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    offset = 0;
                    trunggian = 5;
                    break;
                case DayOfWeek.Tuesday:
                    offset = -1;
                    trunggian = 4;
                    break;
                case DayOfWeek.Wednesday:
                    offset = -2;
                    trunggian = 3;
                    break;
                case DayOfWeek.Thursday:
                    offset = -3;
                    trunggian = 2;
                    break;
                case DayOfWeek.Friday:
                    offset = -4;
                    trunggian = 1;
                    break;
                case DayOfWeek.Saturday:
                    offset = -5;
                    trunggian = 0;
                    break;
                case DayOfWeek.Sunday:
                    offset = -6;
                    trunggian = 0;
                    break;
            }

            stDate = dDateTime.AddDays(offset);
            endDate = dDateTime.AddDays(5 + offset);

            if (Var == "0")
                return stDate;
            else
                return endDate;
        }

        private void btnUngdung_Click(object sender, EventArgs e)
        {
            if (grdDS.RowCount == 0)
            {
                XtraMessageBox.Show("Dữ liệu chưa tồn tại");
                return;
            }
            if (datTu.DateTime > datDen.DateTime)
            {
                XtraMessageBox.Show("Hãy chọn lại vùng ngày tháng");
                return;
            }
            LoadArrDatetime(datTu.DateTime, datDen.DateTime);
            if (ClsThucdon_monanbll.DataSet_ThucdontuanSelectAllByBeginDateToEndDatime(BgDate, EndDate).Tables[0].Rows.Count == 0)
            {
                XtraMessageBox.Show("Hãy chọn lại vùng để có dữ liệu");
                return;
            }
            // kieem tra du lieu cua tuan hien tai
            LoadArrDatetime(DateTime.Now, DateTime.Now);
            if (ClsThucdon_monanbll.DataSet_ThucdontuanSelectAllByBeginDateToEndDatime(BgDate, EndDate).Tables[0].Rows.Count > 0)
            {
                XtraMessageBox.Show("Tuần hiện tại đang có dữ liệu, hệ thống không cho thao tác");
                return;
            }
            DataTable Tbl = ClsThucdon_monanbll.Tbl_Khoilop();
            DateTime Dtime = DateTime.Now;
            Dtime = GetDates(Dtime, "0");
            for (int iKhoi = 0; iKhoi < Tbl.Rows.Count; iKhoi++)
            {
                DataTable TblReturn = new DataTable();
                TblReturn = this.ClsThucdon_monanbll.BingSource_DataGridTrunggian((int)Tbl.Rows[iKhoi]["Id"], DateTime.Parse(grdDS.GetFocusedRowCellValue("Tungay").ToString()));
                try
                {
                    for (int i = 0; i < TblReturn.Rows.Count; i++)
                    {
                        int iDatetime = 0;
                        for (int j = 3; j <= TblReturn.Columns.Count - 1; j++)
                        {
                            try
                            {
                                int iBuaantrongngayId = int.Parse(TblReturn.Rows[i]["BuaantrongngayId"].ToString());
                                // kiểm tra nếu chưa có thì thêm mới
                                string Giatri = TblReturn.Rows[i][j].ToString().Replace("\r", " ");
                                if (Giatri.Trim() == "")
                                    continue;
                                ClsThucdon_monanbll.Add(Dtime.AddDays(iDatetime), iBuaantrongngayId, (int)Tbl.Rows[iKhoi]["Id"], Giatri);
                                iDatetime = iDatetime + 1;
                            }
                            catch { }
                        }
                    }
                }
                catch { }
            }
            XtraMessageBox.Show("Cập nhật thành công");
        }

        private void datTu_EditValueChanged(object sender, EventArgs e)
        {
            datDen_EditValueChanged(sender, e);
        }

        private void datDen_EditValueChanged(object sender, EventArgs e)
        {
            if (datTu.DateTime > datDen.DateTime)
            {
                return;
            }
            else
            {
                LoadArrDatetime(datTu.DateTime, datDen.DateTime);
                ClsThucdon_monanbll.ThucdontuanSelectAllByBeginDateToEndDatime(grdDS, BgDate, EndDate);
                cboKhoi_EditValueChanged(sender, e);
            }
        }

        private void btnHienthi_Click(object sender, EventArgs e)
        {
            try
            {
                LoadArrDatetime(ClsThucdon_monanbll.ThucdontuanSelectAllByEndDatime(), DateTime.Now);
                ClsThucdon_monanbll.ThucdontuanSelectAllByBeginDateToEndDatime(grdDS, BgDate, EndDate);
                cboKhoi_EditValueChanged(sender, e);
            }
            catch { }
        }

        private void grdDS_Click(object sender, EventArgs e)
        {
            cboKhoi_EditValueChanged(sender, e);
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            try
            {
                string[] Tg = grdHienthi.Columns["Thu2"].Caption.Split(',');
                string[] Tg2 = Tg[1].Split('-');
                string tt, nn;
                if (int.Parse(Tg2[1].ToString()) < 10)
                {
                    tt = "0" + Tg2[1].ToString();
                }
                else
                { tt = Tg2[1].ToString(); }

                if (int.Parse(Tg2[0].ToString()) < 10)
                {
                    nn = "0" + Tg2[0].ToString();
                }
                else
                { nn = Tg2[0].ToString(); }
                DateTime _dNgay = DateTime.Parse(nn + "/" + tt + "/" + Tg2[2].ToString());
                //       ClsExel.ExportThucdontrongtuan(_dNgay);
                if (ClsTblThamsodauvaobll.SelectAll().Rows.Count > 0 && ClsTblThamsodauvaobll.SelectAll().Rows[0]["Baoanchitiet"].ToString() != "True")

                    ClsExel.ExportThucdontrongtuan_truong(_dNgay, int.Parse(UserComboBoxItem.GetItemValue(cboKhoi)), cboKhoi.Text);
                else
                    ClsExel.ExportThucdontrongtuan(_dNgay);
            }
            catch { }
        }
    }
}